package com.ruoyi.web.controller.energy;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.EnergyRecord;
import com.ruoyi.system.mapper.EnergyRecordMapper;
import com.ruoyi.system.service.req.EnergyRecordAreaReq;
import com.ruoyi.system.service.req.GarbageDataReq;
import com.ruoyi.system.service.resp.EnergyRecordAreaResp;
import com.ruoyi.system.service.resp.EnergyRecordDTO;
import com.ruoyi.system.service.resp.EnergyRecordRespDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author: ruohao
 * @Date: 2023/11/19
 **/
@RestController
@RequestMapping("/energy/record/data")
public class DataController extends BaseController {
    @Resource
    private EnergyRecordMapper energyRecordMapper;

    @GetMapping("/compare")
    public AjaxResult compare(EnergyRecordDTO energyRecordDTO) {
        List<EnergyRecordRespDTO> energyRecordRespDTOS = new ArrayList<>();
        EnergyRecordRespDTO energyRecordRespDTO = new EnergyRecordRespDTO();
        EnergyRecordRespDTO energyRecordRespDTO1 = new EnergyRecordRespDTO();
        energyRecordRespDTO.setName("二区" + energyRecordDTO.getType() + "数据");
        energyRecordRespDTOS.add(energyRecordRespDTO);
        energyRecordRespDTO1.setName("一区" + energyRecordDTO.getType() + "数据");
        energyRecordRespDTOS.add(energyRecordRespDTO1);
        String typeInDb = energyRecordDTO.getType().substring(0, 1);
        List<EnergyRecord> energyRecords = energyRecordMapper.selectList(EnergyRecord.builder().type(typeInDb)
                .startTime(energyRecordDTO.getStartTime()).endTime(energyRecordDTO.getEndTime()).build());
        logger.info("compare 数据 {}", JSON.toJSONString(energyRecords));
        BigDecimal value = energyRecords.stream().filter(e -> "二区".equals(e.getArea()))
                .map(EnergyRecord::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal value1 = energyRecords.stream().filter(e -> "一区".equals(e.getArea()))
                .map(EnergyRecord::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
        energyRecordRespDTO.setValue(value.longValue());
        energyRecordRespDTO1.setValue(value1.longValue());
        return success(energyRecordRespDTOS);
    }

    @GetMapping("/area")
    public AjaxResult area(EnergyRecordAreaReq energyRecordAreaReq) {
        EnergyRecordAreaResp resp = new EnergyRecordAreaResp();
        List<String> timeSeries = getAreaTimeSeries(energyRecordAreaReq.getEndTime());
        resp.setTimeSeries(timeSeries);
        resp.setKeySeries(getAreaKeySeries());
        resp.setData(getAreaData(timeSeries, getAreaKeySeries(), energyRecordAreaReq.getArea(), energyRecordAreaReq.getLocation()));
        return success(resp);
    }

    private List<String> getAreaTimeSeries(String endTime) {
        List<String> result = new ArrayList<>();
        int year;
        Calendar calendar = Calendar.getInstance();
        if (StringUtils.isEmpty(endTime)) {
            year = calendar.get(Calendar.YEAR);

        } else {
            year = Integer.parseInt(endTime);
        }
        result.add((year - 2) + "");
        result.add((year - 1) + "");
        result.add(year + "");
        return result;
    }

    private List<List<Integer>> getAreaData(List<String> timeSeries, List<String> areaKeySeries, String area, String location) {
        List<List<Integer>> result = new ArrayList<>();
        for (String areaKeySery : areaKeySeries) {
            List<Integer> one = new ArrayList<>();
            for (String timeSery : timeSeries) {
                String startTime = timeSery + "-01-01 00:00:00";
                String endTime = timeSery + "-12-31 23:59:59";
                Integer amountByArea = energyRecordMapper.getAmountByArea(startTime, endTime, area, location, areaKeySery);
                one.add(amountByArea == null ? 0 : amountByArea);
            }
            result.add(one);
        }
        return result;
    }

    private List<String> getAreaKeySeries() {
        List<String> result = new ArrayList<>();
        result.add("水");
        result.add("电");
        result.add("气");
        result.add("热");
//        result.add("供暖");
        return result;
    }

    @GetMapping("/line")
    public AjaxResult line(EnergyRecordAreaReq energyRecordAreaReq) {
        EnergyRecordAreaResp resp = new EnergyRecordAreaResp();
        resp.setTimeSeries(getZHTimeSeries());
        resp.setKeySeries(getAreaKeySeries());
        resp.setData(getLineData(getZHTimeSeries(), getAreaKeySeries(), energyRecordAreaReq.getLocation(), energyRecordAreaReq.getStartTime()));
        return success(resp);
    }

    private List<String> getZHTimeSeries() {
        List<String> result = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            result.add(i + "月");
        }
        return result;
    }

    private List<String> getLineKeySeries() {
        List<String> result = new ArrayList<>();
        result.add("二区");
        result.add("一区");
        return result;
    }

    private List<List<Integer>> getLineData(List<String> timeSeries, List<String> areaKeySeries, String location, String year) {
        List<List<Integer>> result = new ArrayList<>();
        for (String areaKeySery : areaKeySeries) {
            List<Integer> one = new ArrayList<>();
            for (String timeSery : timeSeries) {
                timeSery = timeSery.replace("月", "");
                Calendar calendar = Calendar.getInstance();
                if (StringUtils.isEmpty(year)) {
                    int i = calendar.get(Calendar.YEAR);
                    year = i + "";
                }
                calendar.set(Integer.parseInt(year), Integer.parseInt(timeSery) - 1, 1); // 设置年份和月份
                int lastDay = calendar.getActualMaximum(Calendar.DAY_OF_MONTH); // 获取该月的最大天数
                String startTime = year + "-" + timeSery + "-01 00:00:00";
                String endTime = year + "-" + timeSery + "-" + lastDay + " 23:59:59";
                Integer amountByArea = energyRecordMapper.getAmountByArea(startTime, endTime, null, location, areaKeySery);
                one.add(amountByArea == null ? 0 : amountByArea);
            }
            result.add(one);
        }
        return result;
    }

    private List<String> getGarbageKeySeries() {

        List<String> result = new ArrayList<>();
        result.add("回收垃圾");
        result.add("其他垃圾");
        result.add("厨余垃圾");
        result.add("有害垃圾");
        return result;
    }


    @GetMapping("/garbage")
    public AjaxResult garbage(GarbageDataReq garbageDataReq) {
        EnergyRecordAreaResp resp = new EnergyRecordAreaResp();
        resp.setTimeSeries(getTimeSeries());
        resp.setKeySeries(getGarbageKeySeries());
        resp.setData(getGrabageData());
        return success(resp);
    }

    private List<String> getTimeSeries() {
        List<String> result = new ArrayList<>();
        result.add("2012");
        result.add("2013");
        result.add("2014");
        result.add("2015");
        result.add("2016");
        return result;
    }

    private List<List<Integer>> getGrabageData() {
        List<Integer> one = new ArrayList<>();
        one.add(1);
        one.add(2);
        one.add(3);
        one.add(4);
        one.add(5);
        List<List<Integer>> result = new ArrayList<>();
        result.add(one);
        result.add(one);
        result.add(one);
        result.add(one);
        return result;
    }

    private Map<String, List<Long>> getData(GarbageDataReq req) {
        Map<String, List<Long>> data = new HashMap<>();
        List<Long> waterData = new ArrayList<>();
        waterData.add(1L);
        waterData.add(2L);
        waterData.add(3L);
        waterData.add(4L);
        waterData.add(5L);
        data.put("回收垃圾", waterData);
        List<Long> eleData = new ArrayList<>();
        eleData.add(1L);
        eleData.add(2L);
        eleData.add(3L);
        eleData.add(4L);
        eleData.add(5L);
        data.put("其他垃圾", waterData);
        List<Long> airData = new ArrayList<>();
        airData.add(1L);
        airData.add(2L);
        airData.add(3L);
        airData.add(4L);
        airData.add(5L);
        data.put("厨余垃圾", waterData);
        List<Long> hotData = new ArrayList<>();
        hotData.add(1L);
        hotData.add(2L);
        hotData.add(3L);
        hotData.add(4L);
        hotData.add(5L);
        data.put("有害垃圾", waterData);
        return data;
    }

    private Map<String, List<Long>> getData(EnergyRecordAreaReq req) {
        Map<String, List<Long>> data = new HashMap<>();
        List<Long> waterData = new ArrayList<>();
        waterData.add(1L);
        waterData.add(2L);
        waterData.add(3L);
        waterData.add(4L);
        waterData.add(5L);
        data.put("水", waterData);
        List<Long> eleData = new ArrayList<>();
        eleData.add(1L);
        eleData.add(2L);
        eleData.add(3L);
        eleData.add(4L);
        eleData.add(5L);
        data.put("电", waterData);
        List<Long> airData = new ArrayList<>();
        airData.add(1L);
        airData.add(2L);
        airData.add(3L);
        airData.add(4L);
        airData.add(5L);
        data.put("气", waterData);
        List<Long> hotData = new ArrayList<>();
        hotData.add(1L);
        hotData.add(2L);
        hotData.add(3L);
        hotData.add(4L);
        hotData.add(5L);
        data.put("热", waterData);
        return data;
    }


}
